﻿Public Class PENNAME
    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection
    Dim PROVIDER_MASPENNAME As New PROVIDERSCLASS

    Private Sub filterSex()
        Dim s2 As FILTERCLASS
        Dim sql2 As String = "SELECT CAST(SEX AS CHAR(8)) AS SEX, `SEXDESC`, `SEXDESC_EN` FROM massex WHERE STATUS = '1';"
        s2 = New FILTERCLASS(TxtSexFilter, sql2, "รหัสเพศ,ไทย,อังกฤษ", "50,160,160", "1,1,1", "1,1,1")
        's.SetShowInDown = False
        s2.SetShowBorder = True
    End Sub

    Private Sub PENNAME_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Dim selectcombobox As New maspenname
        'selectcombobox.loadcombobox(SEXCmb)
        'SEXCmb.SelectedValue = -1

        filterSex()

        SelectDatagridpenname()

        DesignForm(Me)
        DesignGrid(MASPENNAMEGrid)

        PROVIDER_MASPENNAME.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub SelectDatagridpenname()
        Try
            Dim objSelectDatamaspenname As New maspenname
            Dim MASPENNAME As DataTable
            MASPENNAME = connect.GetTable(objSelectDatamaspenname.selectMaspenname_)
            MASPENNAMEGrid.PrimaryGrid.DataSource = MASPENNAME
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub MASPENNAMEGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles MASPENNAMEGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสนามแฝง").Value() IsNot DBNull.Value Then
            TxtSexFilter.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เพศ").Value()
            TxtSexFilter.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("SEX").Value()
        Else
            TxtSexFilter.Clear()
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสนามแฝง").Value() IsNot DBNull.Value Then
            PENID.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสนามแฝง").Value()
        Else
            PENID.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อนามแฝง").Value() IsNot DBNull.Value Then
            PENNAMETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อนามแฝง").Value()
        Else
            PENNAMETxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("นามสกุลแฝง").Value() IsNot DBNull.Value Then
            PENLNAMETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("นามสกุลแฝง").Value()
        Else
            PENLNAMETxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("SEX").Value() IsNot DBNull.Value Then
            SEXCmb.SelectedValue = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("SEX").Value()
        Else
            SEXCmb.SelectedValue = -1
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            pennamestatusCheckbox.Checked = True
        Else
            pennamestatusCheckbox.Checked = False
        End If
        connect = CONDB.NewConnection()
        If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
            Try
                Dim Sql As String
                connect.BeginTrans()
                Sql = "UPDATE maspenname SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`status` = 1 "
                Else
                    Sql += "`status` = 0 "
                End If
                Sql += "WHERE `penid` = '" & PENID.Text & "' ;"
                connect.ExecuteNonQuery(Sql)
                connect.CommitTrans()
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
            End Try
        End If
        connect.Dispose()
        'connectlocal = conlocal.NewConnection()
        'If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
        '    Try
        '        Dim Sql As String
        '        connectlocal.BeginTrans()
        '        Sql = "UPDATE MASPENNAME SET "
        '        If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
        '            Sql += "[STATUS] = 1 "
        '        Else
        '            Sql += "[STATUS] = 0 "
        '        End If
        '        Sql += "WHERE [ID] = '" & PENID.Text & "' ;"

        '        connectlocal.ExecuteNonQuery(Sql)
        '        connectlocal.CommitTrans()
        '    Catch ex As Exception
        '        connectlocal.RollbackTrans()
        '        MsgBox(ex.ToString)
        '    End Try
        'End If
        'connectlocal.Dispose()
        addPenname.Enabled = False
    End Sub

    Private Sub addPenname_Click(sender As Object, e As EventArgs) Handles addPenname.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASPENNAME.PADD_ = True Then
                Dim objInsertData As New maspenname
                objInsertData.PENNAME_ = PENNAMETxt.Text
                objInsertData.PENLNAME_ = PENLNAMETxt.Text
                objInsertData.SEX_ = TxtSexFilter.Tag
                objInsertData.pennameStatus_ = pennamestatusCheckbox.Checked
                objInsertData.insertMaspennameData()

                SelectDatagridpenname()

                PENNAMETxt.Text = ""
                pennamestatusCheckbox.Checked = False
                SEXCmb.SelectedValue = -1
            Else
                MsgBox(PROVIDER_MASPENNAME.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editPenname_Click(sender As Object, e As EventArgs) Handles editPenname.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASPENNAME.PUPD_ = True Then
                Dim editStat As Integer
                editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลสถานะดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                If editStat = 1 Then
                    Dim objEditData As New maspenname
                    objEditData.pennameID_ = PENID.Text
                    objEditData.PENNAME_ = PENNAMETxt.Text
                    objEditData.PENLNAME_ = PENLNAMETxt.Text
                    objEditData.SEX_ = TxtSexFilter.Tag
                    objEditData.pennameStatus_ = pennamestatusCheckbox.Checked
                    objEditData.editMaspennameData()

                    SelectDatagridpenname()

                    PENNAMETxt.Text = ""
                    PENID.Text = ""
                    pennamestatusCheckbox.Checked = False
                    SEXCmb.SelectedValue = -1
                End If
                addPenname.Enabled = True
            Else
                MsgBox(PROVIDER_MASPENNAME.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL(maspenname.Text)
        nextform.ShowDialog()
    End Sub

    Private Sub ButtonItem1_Click(sender As Object, e As EventArgs) Handles ButtonItem1.Click
        Dim nextform As New IMPORTEXCEL(maspenname.Text)
        nextform.ShowDialog()
    End Sub
End Class